"use strict"

module.exports = function(db, options){
  var fs     = require("fs");
  var _      = require("lodash");
  var path   = require("path");
  var models = {};

  fs
    .readdirSync(__dirname)
    .filter(function(file) {
      return (file.indexOf(".") !== 0) && (file !== "index.js");
    })
    .forEach(function(file) {
      var model = db.sequelize.import(path.join(__dirname, file));
      models[model.name] = model;
    });

  //merge moduleDb into db
  //TODO: should we check if there is any Key conflicts?
  _.assign(db, models);

  Object.keys(models).forEach(function(modelName) {
    if ("associate" in models[modelName]) {
      models[modelName].associate(db);
    }
  });

  return db;
}
